<template>
	<view class="settle_box">
		<view class="radio_box">
			<radio color="#409eff" :checked="checkAll" @click="allCheck" /><text>全选</text>
		</view>
		<view class="total_box">
			<text>合计：<text class="data">￥{{ getPrice }}</text> </text>
		</view>
		<view class="settle" @click="settlement">结算({{ checkCount }})</view>
	</view>
</template>

<script>
	import { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
		name:"my-settle",
		data() {
			return {
				sec: 3,
				timer: null
			};
		},
		onReady() {
			this.isCheckAll();
		},
		filters: {
			
		},
		computed: {
			...mapState('store_cart', ['checkAll', 'cart']),
			...mapState('store_user', ['token', 'userInfo']),
			...mapGetters('store_cart', ['checkCount', 'getPrice']),
			...mapGetters('store_user', ['getAddr'])
			
		},
		methods: {
			...mapMutations('store_cart', ['isCheckAll', 'changeCheckState']),
			...mapMutations('store_user', ['updateRedirectInfo']),
			
			// 点击【全选】按钮触发
			allCheck() {
				this.changeCheckState(!this.checkAll)
				this.$emit('allCheck', { state: this.checkAll })
			},
			
			// 显示提示信息
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '正在前往登录 ' + n + '...',
					mask: true,
					duration: 1500
				})
			},
			
			// 延迟跳转到登陆页面
			delayNavigate() {
				this.sec = 3
				this.showTips(this.sec)
				
				// 创建定时器， 每个一秒执行一次
				this.timer = setInterval(() => {
					this.sec --
					
					if (this.sec <= 0) {
						clearInterval(this.timer)
						uni.switchTab({ 
							url: '/pages/person/person',
							success: () => {
								this.updateRedirectInfo({
									openType: 'swichTab',
									from: '/pages/cart/cart'
								})
							}
						})
						return
					}
					
					this.showTips(this.sec)	
				}, 1000)
			},
			
			// 微信支付方法
			async payOrder() {
				// 1. 创建订单
				// 1.1 组织订单的信息对象
				const orderInfo = {
					order_price: 0.01,
					consignee_addr: this.getAddr,
					goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
				}
				
				// 1.2 请求创建订单
				const {data: res} = await uni.$http.post('https://www.escook.cn/api/public/v1/my/orders/create', orderInfo)
				console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				
				// 1.3 得到服务器响应的订单编号
				const order_number = res.message.order_number
				console.log('订单编号 : ', orderNumber)
					 
				// 2. 订单预支付
				// 2.1 请求获取订单的支付信息
				const {data: res2} = await uni.$http.post('https://www.escook.cn/api/public/v1/my/orders/req_unifiedorder', {order_number})
				// 2.2 预支付订单生成失败
				if (res2.meta.status !== 200) return uni.$showMsg('预支付订单生成失败！')
				// 获取订单支付的相关参数
				const payInfo = res2.message.pay
				console.log('预支付订单 : ', payInfo)
				
				// 3. 发起微信支付
				// 3.1 调用 uni.requestPayment() 发起微信支付
				const [err, success] = await uni.requestPayment(payInfo)
				// 3.2 未完成支付
				if (err) return uni.$showMsg('支付失败')
				// 3.3 完成支付， 进一步查询支付结果
				const {data: res3} = await uni.$http.post('https://www.escook.cn/api/public/v1/my/orders/chkOrder', {order_number})
				// 3.4 检测到订单支付状态
				res3.meta.status !== 200 ? uni.$showMsg('订单未支付') : uni.showToast({
					title: '支付完成',
					icon: 'success'
				})
				
			},
			
			// 点击【结算】按钮触发
			settlement() {
				// 判断用户登录状态、用户是否填写收获地址、用户是否选择商品
				this.userInfo.nickName ? console.log('用户登录了') : this.delayNavigate() 
				this.checkCount ? console.log('用户选择商品了') : uni.$showMsg('请选择购买的商品...')  
				this.getAddr ? console.log('用户填写地址了') : uni.$showMsg('请选择收货地址...')  
				
				// 调用微信支付
				this.payOrder()
			}
		}
	}
</script>

<style lang="scss">
	.settle_box {
		width: 100%;
		// border: 1px solid #000;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		background-color: #FFFFFF;
		.radio_box {
			padding-left: 10rpx;
		}
		.total_box {
			.data {
				color: #409eff;
				font-weight: bold;
			}
		}
		.settle {
			background-color: #409eff;
			padding: 10px 20px;
			color: #FFFFFF;
		}
	}
</style>
